﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySkiPatrolEntity;
using System.Data;
using MySkiPatrolSQL;


namespace MySkiPatrolDAL
{
    public class SignInDAO
    {
        SqlConnector SQLCONN = new SqlConnector();

        public SignInDAO()
        {

        }
        
        public ScanResponse GetEmployeeIDInfo(ScanResponse data)
        {
            try
            {
                DataSet ds = SQLCONN.RunSQL_DataSet(SignInSQL.GetEmployeeIDInfo(data), "temp");
                DataTable dt = ds.Tables["temp"];

                if (dt.Rows.Count == 1)
                {
                    data.MemberID = DataConvertor.toString(dt.Rows[0], "memberid");
                    data.MemberName = DataConvertor.toString(dt.Rows[0], "firstname") + ' ' + DataConvertor.toString(dt.Rows[0], "lastname");
                    
                }
                else
                {

                }
            }
            catch (Exception ex)
            {

            }

            return data;
        }

        public void AssignIdToMember(ScanResponse data)
        {
            DataSet ds = SQLCONN.RunSQL_DataSet(SignInSQL.AssignIdToMember(data), "temp");
            DataTable dt = ds.Tables["temp"];
        }

        public ScanResponse GetEmployeeInfo(ScanResponse data)
        {
            try
            {
                DataSet ds = SQLCONN.RunSQL_DataSet(SignInSQL.GetEmployeeInfo(data), "temp");
                DataTable dt = ds.Tables["temp"];

                if (dt.Rows.Count == 1)
                {
                    data.MemberID = DataConvertor.toString(dt.Rows[0], "memberid");
                    data.MemberName = DataConvertor.toString(dt.Rows[0], "firstname") + ' ' + DataConvertor.toString(dt.Rows[0], "lastname");
                    
                }
                else
                {

                }
            }
            catch (Exception ex)
            {

            }

            return data;
        }

        

        //public void UpdatePatrollerAssignment(ScanResponse data)
        //{
        //    try
        //    {
        //        DataSet ds = SQLCONN.RunSQL_DataSet(TimeKeeperSQL.UpdatePatrollerAssignment(data), "temp");
        //        DataTable dt = ds.Tables["temp"];
        //    }
        //    catch (Exception ex)
        //    {

        //    }
        //}

        public void AssignIdToEmployee(ScanResponse data)
        {
            DataSet ds = SQLCONN.RunSQL_DataSet(SignInSQL.AssignIdToEmployee(data), "temp");
            DataTable dt = ds.Tables["temp"];
        }

        public void CancelSignin(ScanResponse data)
        {
            DataSet ds = SQLCONN.RunSQL_DataSet(SignInSQL.CancelSignin(data), "temp");
            DataTable dt = ds.Tables["temp"];
        }

        public void SetIDCardLastUsedDate(ScanResponse data)
        {
            DataSet ds = SQLCONN.RunSQL_DataSet(SignInSQL.SetIDCardLastUsedDate(data), "temp");
            DataTable dt = ds.Tables["temp"];
        }

        public List<Member> GetEmployeesWithNoID(Patrol data)
        {
            List<Member> myReturn = new List<Member>();

            try
            {
                DataSet ds = SQLCONN.RunSQL_DataSet(SignInSQL.GetAllMembers(data), "temp");
                DataTable dt = ds.Tables["temp"];

                foreach (DataRow dr in dt.Rows)
                {
                    Member item = new Member();

                    item.ID = DataConvertor.toString(dr, "memberid");
                    item.FirstName = DataConvertor.toString(dr, "firstname");
                    item.LastName = DataConvertor.toString(dr, "lastname");
                    item.FullName = DataConvertor.toString(dr, "firstname") + " " + DataConvertor.toString(dr, "lastname");

                    myReturn.Add(item);
                }
            }
            catch (Exception ex)
            {

            }

            return myReturn;
        }

        public void EmployeeSignOut(EmployeeSignIn data)
        {
            try
            {
                DataSet ds = SQLCONN.RunSQL_DataSet(SignInSQL.EmployeeSignIn(data), "temp");
                DataTable dt = ds.Tables["temp"];
            }
            catch (Exception ex)
            {
                data.DataTransaction = new DataTransaction();
                data.DataTransaction.Success = false;
                data.DataTransaction.TransactionErrors = new List<Error>();
                data.DataTransaction.TransactionErrors.Add(new Error("0000", ex.Message));
            }
        }

        
        public ScanResponse GetEmployeeDailySignin(ScanResponse data)
        {
            try
            {
                DataSet ds = SQLCONN.RunSQL_DataSet(SignInSQL.GetEmployeeDailySignin(data), "temp");
                DataTable dt = ds.Tables["temp"];

                if (dt.Rows.Count == 1)
                {
                    data.SignIn = DataConvertor.toDateTimeOffset(dt.Rows[0], "signin");
                    data.IsSignedIn = true;
                    
                }
                else
                {

                }
            }
            catch (Exception ex)
            {

            }

            return data;
        }

        //public ScanResponse PatrollerDailySignout(ScanResponse data)
        //{
        //    try
        //    {
        //        DataSet ds = SQLCONN.RunSQL_DataSet(TimeKeeperSQL.PatrollerDailySignout(data), "temp");
        //        ds = SQLCONN.RunSQL_DataSet(TimeKeeperSQL.GetPatrollerDailySignin(data), "temp");
        //        DataTable dt = ds.Tables["temp"];

        //        if (dt.Rows.Count == 1)
        //        {
        //            //error...should have signed out..

        //        }
        //        else
        //        {
        //            data.SignOut = DateTime.Now;
        //            data.IsSignedIn = false;
        //        }
        //    }
        //    catch (Exception ex)
        //    {

        //    }

        //    return data;
        //}

        public ScanResponse EmployeeSignin(ScanResponse data)
        {
            try
            {
                DataSet ds = SQLCONN.RunSQL_DataSet(SignInSQL.EmployeeDailySignin(data), "temp");
                ds = SQLCONN.RunSQL_DataSet(SignInSQL.GetEmployeeDailySignin(data), "temp");
                DataTable dt = ds.Tables["temp"];

                if (dt.Rows.Count == 1)
                {
                    data.SignIn = DataConvertor.toDateTime(dt.Rows[0], "signin");
                    data.IsSignedIn = true;
                }
                else
                {

                }
            }
            catch (Exception ex)
            {
                data.DataTransaction = new DataTransaction();
                data.DataTransaction.Success = false;
                data.DataTransaction.TransactionErrors = new List<Error>();
                data.DataTransaction.TransactionErrors.Add(new Error("0000", ex.Message));
            }

            return data;
        }

        public List<ScanResponse> GetDailySignins(Patrol data)
        {
            List<ScanResponse> myReturn = new List<ScanResponse>();

            try
            {
                DataSet ds = SQLCONN.RunSQL_DataSet(SignInSQL.GetDailySignins(data), "temp");
                DataTable dt = ds.Tables["temp"];

                foreach (DataRow dr in dt.Rows)
                {
                    ScanResponse item = new ScanResponse();
                    item.IsSignedIn = true;
                    item.MemberID = DataConvertor.toString(dr, "memberid");
                    item.MemberName = DataConvertor.toString(dr, "FirstName") + " " + DataConvertor.toString(dr, "LastName");
                    item.SignIn = DataConvertor.toDateTimeOffset(dr, "signin");
                    
                    myReturn.Add(item);
                }
            }
            catch (Exception ex)
            {

            }

            return myReturn;
        }

        //public void CloseDailySignins()
        //{
        //    try
        //    {
        //        DataSet ds = SQLCONN.RunSQL_DataSet(TimeKeeperSQL.CloseDailySignins(), "temp");
        //        DataTable dt = ds.Tables["temp"];
        //    }
        //    catch (Exception ex)
        //    {

        //    }
        //}
    }
}
